<?php
if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
include_once( "class_6h/class.lotto6h.php" );
$lotto6h = new lotto6h();
$ui = new UI();
$bettype = 19;
$type = $_POST['type'];
if (intval($type) < 100)
{
    account_err("错误的参数！");
}
$gametype = intval(substr($type, 0, 1));
$gamecount = intval(substr($type, 0 - 2));
if (isset($_POST['cnums']))
{
    $cnums = $_POST['cnums'];
    $arnum = explode(",", $cnums);
    foreach ($arnum as $key => $val)
    {
        $arnum[$key] = trim($val);
    }
    $numcount = count($arnum);
}
else
{
    $arnum = array();
    $numcount = 0;
    $i = 1;
    while ($i < 13)
    {
        if (isset($_POST["num_" . $i]))
        {
            $arnum[$numcount] = $i;
            ++$numcount;
        }
        ++$i;
    }
}
if ($numcount < $gamecount)
{
    account_err("必须选择 " . $gamecount . " 种生肖！");
}
else if ($gamecount < $numcount)
{
    account_err("只能选择 " . $gamecount . " 种生肖！");
}
asort($arnum);
reset($arnum);
$tpl = new Template2();
$tpl->load(array("main" => "proc14.htm"));
$tpl->set("account", $curruser['account']);
if ($curruser['enabled'] == 0 || $curruser['enabled'] == 2)
{
    account_err("帐号已被暂停使用");
}
$rs = getdb("select * from lotto where open>='" . date("Y-m-d") . "' order by open");
if ($rs->eof)
{
    account_err("目前没有可以下注期数！");
}
$open = $rs->f['open'];
$round = $rs->f['round'];
$lotto_id = $rs->f['id'];
$conf = new Config();
$prot_start = $conf->get("protect_start");
$check_6s_alls = intval($conf->get("check_6s_all"));
$stopbet = $conf->get("othebet");
if (date("Y-m-d") < $open)
{
    exit();
}
else if (date("Y-m-d") == $open)
{
    if ($stopbet <= date("Hi"))
    {
        $ui->showErrorPage("<p>下注期数：" . d3($round) . "<br>开奖日：" . $open . "</p>" . "<p class=error>已经截止下注！</p>");
        exit();
    }
    else if (date("Hi") <= $conf->get("startbet"))
    {
        exit();
    }
}
$tpl->set("op", $open);
$tpl->set("round", d3($round));
$tpl->set("lotto_id", $lotto_id);
$tpl->set("account", $curruser['account']);
$tpl->set("amount", $curruser['amount']);
$chktime2 = 0;
$chk_rs = getdb("select createtime from bet where lotto_id=" . $lotto_id . " and user_id=" . $curruser['id'] . " and bettype=" . $bettype . " and rame=0");
while (!$chk_rs->eof)
{
    $chktime2 = $chk_rs->f[0];
    $chk_rs->movenext();
}
$chk_rs->close();
$chktime1 = systime();
if (intval($chktime1) - intval($chktime2) < 12)
{
    account_err("请於 " . ( 12 - ( intval($chktime1) - intval($chktime2) ) ) . "秒後才继续下注！");
}
$rs1 = getdb("select amount_6h from k_user_bet where user_id=" . $curruser['id'] . " and lotto_id=" . $lotto_id);
$usedamount = $rs1->f[0] ? $rs1->f[0] : 0;
$rs1->close();
$tpl->set("usedamount", $usedamount);
$rs1 = getdb("select sum(amount) from bet where bettype={$bettype} and user_id=" . $curruser['id'] . " and lotto_id=" . $lotto_id);
$betamount = $rs1->f[0] ? $rs1->f[0] : 0;
$tpl->set("betamount", $betamount);
$rs1->close();
$betforms = "<input type=hidden name=\"cnums\" value='" . implode(",", $arnum) . "'>\n";
$tpl->set("type", $type);
$tpl->set("betforms", $betforms);
$betname = "合肖";
$tpl->set("betname", $betname);
$err = 0;
$j = 0;
while ($j < $gamecount - 1)
{
    $cz = $arnum[$j];
    if ($cz == "" || substr($cz, 0, 1) == " " || substr($cz, 1) == " ")
    {
        ++$err;
    }
    $i = $j + 1;
    while ($i < $gamecount)
    {
        switch ($arnum[$i])
        {
            case 1 :
            case 2 :
            case 3 :
            case 4 :
            case 5 :
            case 6 :
            case 7 :
            case 8 :
            case 9 :
            case 10 :
            case 11 :
            case 12 :
                if (intval($arnum[$i]) == intval($cz))
                {
                    ++$err;
                }
                break;
            default :
                ++$err;
                break;
        }
        ++$i;
    }
    ++$j;
}
$num = implode(",", $arnum);
if (0 < $err)
{
    write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, "使用不正常方式下注（" . $num . "）");
    account_err("请勿以非正常方式下注！你的 IP 已经被记录并且也通报你的上层管理员了！");
}
$cnum = array();
$ddss1 = 0;
$ddss2 = 0;
$ddxx1 = 0;
$ddxx2 = 0;
$chkx1 = "02、07、08、10、11、12";
$chkx2 = "01、03、04、05、06、09";
$i = 0;
while ($i < $gamecount)
{
    $cnum[$i] = $g_chinayear[$arnum[$i]];
    if ($check_6s_alls == 1 && 1 < $gamecount)
    {
        if ($arnum[$i] % 2 == 0)
        {
            ++$ddss1;
        }
        else
        {
            ++$ddss2;
        }
        if (0 < substr_count($chkx1, d2($arnum[$i])))
        {
            ++$ddxx1;
        }
        else if (0 < substr_count($chkx2, d2($arnum[$i])))
        {
            ++$ddxx2;
        }
    }
    ++$i;
}
if ($check_6s_alls == 1 && 1 < $gamecount)
{
    if ($ddss1 == $gamecount)
    {
        account_err("不能选择全部为 单数 的生肖！");
    }
    if ($ddss2 == $gamecount)
    {
        account_err("不能选择全部为 双数 的生肖！");
    }
    if ($ddxx1 == $gamecount)
    {
        account_err("不能选择全部为 家禽类 的生肖！");
    }
    if ($ddxx2 == $gamecount)
    {
        account_err("不能选择全部为 野兽类 的生肖！");
    }
}
$idisk = getUserValidDisk($curruser);
$disk = $g_disk_code[$idisk];
echo "<input type=hidden name=disk value=" . strpos(" abcd", $disk) . ">";
$tpl->set("diskStr", strtoupper($disk) . "盘");
$rs = getdb("select * from rate1" . $disk . " where lotto_id={$lotto_id}");
if ($rs->eof)
{
    account_err("系统目前维护中，请稍後！");
}
$gtypes = $gametype . "_" . d2($gamecount);
$sub_rates = get_subs_rates($curruser['id'], $lotto_id, 19, $gtypes);
$rate = $rs->f["r19_" . $gtypes] - $sub_rates;
if ($rate == 0)
{
    account_err("没有开放本项目投注！");
}
$ratechange = false;
$betcontent = "<font color=red>" . $gamecount . "肖: " . ( $gametype == 1 ? "中" : "不中" ) . "</font> @ <font color=red id=rate>" . $rate . "</font><br>" . implode(",", $cnum);
$tpl->set("betcontent", $betcontent);
$tpl->set("rates", trimfloat($rate, 1));
$betmin = $conf->get("betmin");
$tpl->set("min", $betmin);
$betmax = $conf->get("betmax");
$tpl->set("max", $betmax);
$tpl->set("max_str", $betmax);
$rs1 = getdb("select * from amounts where bettype=" . $bettype . " and user_id=" . $curruser['id']);
$amount1 = $rs1->f['amount1'] - 0;
$amount2 = $rs1->f['amount2'] - 0;
$tpl->set("amount1", $amount1);
$tpl->set("amount2", $amount2);
$tpl->set("amount1_str", $amount1);
$tpl->set("amount2_str", $amount2-$betamount);
$usemaxamount = $curruser['usemaxamount'];
if (isset($_POST['money']) && $ratechange == false)
{
    $money = intval($_POST['money']);
    if ($money == 0)
    {
        write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, "使用外挂攻击");
        account_err("不要随便攻击本站！你的 IP 已经被记录了！");
    }
    if ($money < $betmin)
    {
        account_err("下注金额不能小於最低限额！");
    }
    if ($amount1 < $money)
    {
        account_err("下注金额不能超过单注限额！");
    }
    if ($amount2 < $money + $betamount)
    {
        account_err("下注金额不能超过单项限额！");
    }
    if ($curruser['balance'] < $money)
    {
        account_err("下注金额不能超过信用额度！");
    }
    if ($usemaxamount && $curruser['maxamount'] < $money)
    {
        account_err("下注金额不能超过本期限额！");
    }
    if ($conf->get("othebet") <= date("Hi"))
    {
        account_err("已经截止下注！");
    }
    $stime = systime();
    $payoff = round($money * ( $rate - 1 ));
    $numbers = $gtypes . "|" . $num;
    $fields = array();
    $fields['user_id'] = $curruser['id'];
    $fields['lotto_id'] = $lotto_id;
    $fields['createtime'] = $stime;
    $fields['bettype'] = $bettype;
    $fields['numbers'] = "'{$numbers}'";
    $fields['amount'] = $money;
    $fields['rate'] = $rate;
    $fields['payoff'] = $payoff;
    $fields['disk'] = $idisk;
    $fields['line_nums'] = "'" . $num . "'";
    $fields['unitmoney'] = $money;
    $u2 = get_user($curruser['prev_id']);
    $u3 = get_user($u2['prev_id']);
    $u4 = get_user($u3['prev_id']);
    $u5 = get_user($u4['prev_id']);
    $fields['acl2'] = $u2['id'];
    $fields['acl3'] = $u3['id'];
    $fields['acl4'] = $u4['id'];
    $fields['acl5'] = $u5['id'];
    $a1 = GetBack($curruser['id'], $fields['bettype'], $idisk, 1);
    $a2 = GetBack($u2['id'], $fields['bettype'], $idisk, 2);
    $a3 = GetBack($u3['id'], $fields['bettype'], $idisk, 3);
    $a4 = GetBack($u4['id'], $fields['bettype'], $idisk, 4);
    $a5 = GetBack($u5['id'], $fields['bettype'], $idisk, 5);
    $fields['back1'] = $a1;
    $fields['back2'] = $a2;
    $fields['back3'] = $a3;
    $fields['back4'] = $a4;
    $fields['back5'] = $a5;
    $a6 = GetBack($u5['supe_account'], $fields['bettype'], $idisk, 255);
    $fields['back6'] = $a6;
    $protmod = $u5['protmod'];
    $fields['mpercent'] = $u2['mpercent'];
    $fields['percent2'] = $u2['percent0'];
    $fields['percent3'] = min($u3['percent1'], $u4['mpercent'] - $fields['mpercent'] - $fields['percent2']);
    $fields['percent4'] = min($u4['percent1'], $u5['mpercent'] - $fields['mpercent'] - $fields['percent2'] - $fields['percent3']);
    $fields['upercent'] = 100 - $fields['mpercent'] - $fields['percent2'] - $fields['percent3'] - $fields['percent4'];
    if ($fields['percent3'] < 0 || $fields['percent4'] < 0 || $fields['upercent'] < 0)
    {
        err("占成参数设置错误，注单无法存档！");
        return;
    }
    $fields['r2'] = $money * $fields['percent2'] / 100;
    $fields['r3'] = $money * $fields['percent3'] / 100;
    $fields['r4'] = $money * $fields['percent4'] / 100;
    $fields['r6'] = $money * $fields['upercent'] / 100;
    $fields['ip'] = "'" . getIP() . "'";
    $newid = db_insert("bet", $fields);
    if (!$newid)
    {
        err("超过信用额度");
    }
	$rs=getdb("select user_id from k_user_bet where user_id='".$curruser['id']."' and lotto_id='$lotto_id'");
	if(!$rs->eof)
		getdb("update k_user_bet set amount_6h=amount_6h+".$money." where user_id='".$curruser['id']."' and lotto_id='$lotto_id'");
	else
		getdb("insert into k_user_bet set amount_6h=amount_6h+".$money." ,user_id='".$curruser['id']."',lotto_id='$lotto_id'");
    check_autorate($lotto_id, $bettype, "{$numbers}", $money * ( $fields['upercent'] / 100 ));
    $lotto6h->getBet_6h($newid);
    $tpl->load("succ", "succ.htm");
    $tpl->set("ordertype", $betcontent);
    $tpl->set("money", $money);
    $tpl->set("payoff", $payoff);
	$tpl->set("myscript", "parent.mainFrame.location.reload();");
    $tpl->show("succ");
}
else
{
    if ($ratechange == true)
    {
        if (isset($_POST['money']))
        {
            $money = intval($_POST['money']);
            $tpl->set("money", $money);
        }
        $tpl->set("message", "<tr><td colspan=2 bgcolor=red align=center><font color=white>赔率已经变动！</font></td></tr>");
    }
    $tpl->show("main");
}
?>